package com.itjeffrey.autocode.util;

import com.itjeffrey.autocode.constant.SysConstant;
import com.itjeffrey.autocode.enums.ExceptionEnum;
import com.itjeffrey.autocode.exception.ACException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 操作数据库信息的工具
 *
 * @From: Jeffrey
 * @Date: 2020/12/24
 */
@Component
@Slf4j
public class DBUtil {

    public Map<String, String> typeMap = new HashMap<>();

    {
        //读取properties文件
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(System.getProperty(SysConstant.USER_DIR) + SysConstant
                            .TYPE_TRANS_ABSTRACT_PATH)));
            String len = "";
            while ((len = bufferedReader.readLine()) != null) {
                //为typeMap初始化
                typeMap.put(len.substring(0, len.indexOf(SysConstant.SPECIAL_CHAR_14)), len.substring(len.indexOf
                        (SysConstant.SPECIAL_CHAR_14) + 1));
            }
            log.debug("初始化数据库字段类型与Java属性类型完成，匹配个数：{}", typeMap.size());
        } catch (IOException e) {
            log.error("初始化字段属性类型转换失败！");
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 根据数据库字段类型获取相应的java类型
     *
     * @param column
     * @return
     */
    public String columnToJavaClass(String column) {
        if (column == null) {
            return null;
        }
        Set<String> set = typeMap.keySet();
        for (String key : set) {
            if (column.equals(key)) {
                return typeMap.get(key);
            } else if (column.contains(key)) {
                return typeMap.get(key);
            }
        }
        return null;
    }


    /**
     * 对数据库表名或字段名进行驼峰命名
     *
     * @param columnName
     * @return
     */
    public String camelNameForColumn(String columnName) {
        if (columnName == null) {
            throw new ACException(ExceptionEnum.COLUMN_NAME_IS_NULL);
        }
        //字段包含数字则不转换
        if (columnName.matches("[0-9]")) {
            return columnName;
        }
        //字段包含大写则不转换
        if (columnName.matches("[A-Z]")) {
            return columnName;
        }
        if (!columnName.contains(SysConstant.SPECIAL_CHAR_10)) {
            return columnName;
        } else { //去除下划线
            String result = "";
            String[] strs = columnName.split(SysConstant.SPECIAL_CHAR_10);
            for (int i = 0; i < strs.length; i++) {
                if (i == 0) {
                    result = result + strs[i];
                    continue;
                }
                result = result + (char) (strs[i].charAt(0) - 32) + strs[i].substring(1);
            }
            return result;
        }
    }

    /**
     * 根据原始表名获取控制器映射url
     *
     * @param originTableName
     * @return
     */
    public String getMappingUrl(String originTableName) {
        String mappingUrl = SysConstant.SPECIAL_CHAR_16 + originTableName;
        if (originTableName == null) {
            throw new ACException(ExceptionEnum.TABLE_NAME_IS_NULL);
        }
        //去除下划线
        String result = "";
        if (originTableName.contains(SysConstant.SPECIAL_CHAR_10)) {
            String[] strs = originTableName.split(SysConstant.SPECIAL_CHAR_10);
            for (String str : strs) {
                String i = SysConstant.SPECIAL_CHAR_16;
                result = result + i + str;
            }
            mappingUrl = result;
        }
        return mappingUrl;
    }
}
